import org.hibernate.Session;
import org.hibernate.ejb.AbstractEntityManagerImpl;
import org.hibernate.id.IdentifierGeneratorHelper;
import org.hibernate.internal.SessionImpl;
import org.hibernate.type.Type;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws SQLException {

        //1. 创建 EntitymanagerFactory
        String persistenceUnitName = "jpa-1";

        Map<String, Object> properites = new HashMap<String, Object>();
        properites.put("hibernate.show_sql", true);

        EntityManagerFactory entityManagerFactory =
                //Persistence.createEntityManagerFactory(persistenceUnitName);
                Persistence.createEntityManagerFactory(persistenceUnitName, properites);

        //2. 创建 EntityManager. 类似于 Hibernate 的 SessionFactory
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        //3. 开启事务
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        //4. 进行持久化操作
        Customer customer = new Customer();
        customer.setAge(12);
        customer.setEmail("tom@atguigu.com");
        customer.setLastName("Tom");

        entityManager.persist(customer);

		//测试获取主键值
		//testGetGeneratedKeys(entityManager);

        //5. 提交事务
        transaction.commit();

        //6. 关闭 EntityManager
        entityManager.close();

        //7. 关闭 EntityManagerFactory
        entityManagerFactory.close();
    }

	/**
	 * 测试获取主键值
	 * @param entityManager
	 * @throws SQLException
	 */
	public static void testGetGeneratedKeys(EntityManager entityManager) throws SQLException {
		ResultSet rs = null;
		int autoIncKeyFromApi = -1;
		Session session = ((AbstractEntityManagerImpl) entityManager).getSession();
		String insertSQL = "insert into JPA_CUTOMERS (age, email, LAST_NAME) values (?, ?, ?)";
		PreparedStatement ps = ((SessionImpl) session).getTransactionCoordinator().getJdbcCoordinator().getStatementPreparer().prepareStatement(insertSQL, 1);
		ps.setInt(1, 33);
		ps.setString(2, "a@qq.com");
		ps.setString(3,"YY");
		ps.execute();
		//session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate(ps);
		rs = ps.getGeneratedKeys();
		System.out.println(rs.getMetaData().getColumnCount());
		if (rs.next()) {
			autoIncKeyFromApi = rs.getInt(1);
		} else {
			// throw an exception from here
		}
		System.out.println(rs.getMetaData().getColumnCount());
		rs.close();
		rs = null;
		System.out.println("Key returned from getGeneratedKeys():" + autoIncKeyFromApi);

		//IdentifierGeneratorHelper.get(ResultSet rs, String "id", Type type);
		//IdentifierGeneratorHelper.get(rs,"id", TYPE);


	}
}
